package com.gdth.bank.widget.image;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.LogUtils;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.luban.CompressionPredicate;
import com.gdth.luban.Luban;
import com.gdth.luban.OnCompressListener;
import com.gdth.luban.OnRenameListener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

/**
 * 图片管理
 * @author arvin
 *
 */
public class Bimp {

	private static class SingletonHolder{
		private static final Bimp instance = new Bimp();
	}

	public static Bimp getInstance(){
		return SingletonHolder.instance;
	}

	private Bimp(){
		bmp = new ArrayList<Bitmap>();
		drr = new ArrayList<TAppFjxxb>();

		max = 0;
		act_bool = true;
	}

	private int max;
	private boolean act_bool;
	private List<Bitmap> bmp;

	//  图片sd地址 上传服务器时把图片调用下面方法压缩后 保存到临时文件夹 图片压缩后小于100KB，失真度不明显
	private List<TAppFjxxb> drr;
	
	// xjc -- 压缩照片 ： 2019 - 2 -27
		public static void setCompressor(final String path,final ImageView imageView) throws IOException{
			File Localfile = new File(path);
			if (!Localfile.exists())
				return ;
			//final ImageView imageView = new ImageView(BankApplication.mInstance);
			Luban.with(BankApplication.mInstance)
					.load(Localfile)
					.ignoreBy(1000)
					.setTargetDir(Localfile.getAbsolutePath())
					.setRenameListener(new OnRenameListener() {
						@Override
						public String rename(String filePath) {
							return "";
						}
					})
					.filter(new CompressionPredicate() {
						@Override
						public boolean apply(String path) {
							return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
						}
					})
					.setCompressListener(new OnCompressListener() {
						@Override
						public void onStart() {
							// TODO 压缩开始前调用，可以在方法内启动 loading UI
							LogUtils.d("_____", "onStart: ");

						}

						@Override
						public void onSuccess(File file) {
							// TODO 压缩成功后调用，返回压缩后的图片文件
							if(imageView != null) {
								imageView.setImageURI(Uri.fromFile(file));
							}
							//image.setImageBitmap(file)
						}

						@Override
						public void onError(Throwable e) {
							// TODO 当压缩过程出现问题时调用
							LogUtils.d("_____", "onStart: ");
						}
					}).launch();
		}

	/**
	 * 图片压缩
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static Bitmap revitionImageSize(String path) throws IOException {
		File file = new File(path);
		if (!file.exists())
			return null;

		FileInputStream straem = new FileInputStream(file);
		BufferedInputStream in = new BufferedInputStream(straem);
		//int len = readInt(in);
		//byte[] bytes = new byte[len];
		//int readLen = in.read(bytes);

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(in, null, options);
		in.close();
		int i = 0;
		Bitmap bitmap = null;
		BufferedInputStream in1 = null;
		while (true) {
			if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) {
				File file1 = new File(path);
				FileInputStream straem1 = new FileInputStream(file1);
				in1 = new BufferedInputStream(straem1);
				//				int len = readInt(in1);
				//		        byte[] bytes = new byte[len];
				//		        int readLen = in1.read(bytes);
				options.inSampleSize = (int) Math.pow(2.0D, i);
				options.inJustDecodeBounds = false;
				break;
			}
			i += 1;
		}
		bitmap = BitmapFactory.decodeStream(in1, null, options);
		saveBitmapFile(path, bitmap);
		return bitmap;
	}
	
	
	/**
	 *  根据path 压缩保存图片 并返回 Bitmap
	 * @param path 储存路径
	 * @return 
	 */
	public static Bitmap saveBitmapByPath(String path) {
		Bitmap bitmap = null;
		try {
			bitmap = revitionImageSize(path);
			saveBitmapFile(path, bitmap);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}


	/**
	 *  保存Bitmap 到路径 
	 * @param path 路径
	 * @param bitmap  图片
	 */
	public static Bitmap saveBitmapFile(String path, Bitmap bitmap) {
		if(bitmap == null) {
			return null;
		}
		File file = new File(path);
		try { 
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); 
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
			bos.flush(); 
			bos.close(); 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 	
		return bitmap;
	}


	/**
	 * 请取文件长度
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public static int readInt(InputStream in) throws IOException {
		int ch1 = in.read();
		int ch2 = in.read();
		int ch3 = in.read();
		int ch4 = in.read();
		if ((ch1 | ch2 | ch3 | ch4) < 0)
			throw new EOFException();
		return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
	}

	/**
	 * 重置
	 */
	public void reset(){
		this.max = 0;
		this.act_bool = true;
		this.bmp = new ArrayList<Bitmap>();
		this.drr = new ArrayList<TAppFjxxb>();
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}
	
	public void addMax(int number) {
		this.max += number;
	}

	public List<Bitmap> getBmp() {
		return bmp;
	}

	public void setBmp(List<Bitmap> bmp) {
		this.bmp = bmp;
	}

	public List<TAppFjxxb> getDrr() {
		return drr;
	}

	public void setDrr(List<TAppFjxxb> drr) {
		this.drr = drr;
	}

	public int getCount(){
		return this.drr.size();
	}
	
	public int getCountByBmp(){
		return this.bmp.size();
	}

	public boolean isAct_bool() {
		return act_bool;
	}

	public void setAct_bool(boolean act_bool) {
		this.act_bool = act_bool;
	}
}
